import { getUserInfo } from '@/api';

const user = {
  state: {
    id: '',
    avatar: '',
    username: '',
    roles: [],
  },
  mutations: {
    SET_ID: (state, id) => {
      state.id = id;
    },
    SET_USERNAME: (state, username) => {
      state.username = username;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
  },
  actions: {
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then((res) => {
          const user = res.data;
          commit('SET_ID', user.id);
          commit('SET_USERNAME', user.username);
          commit('SET_ROLES', user.roles);
          commit('SET_AVATAR', user.avatar);
          resolve();
        });
      });
    },
  },
};

export default user;
